package com.example.bittraffic;

import java.util.List;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

public class ActivityGoogleMap02 extends MapActivity {
	private static final String TAG = ActivityGoogleMap02.class.toString();
	private static boolean result = false;
	private LocationManager locationManager;
	private String provider;
	private Location location;
	private MapView mapView;
	private MapController mapController;	
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_activity_google_map02);
		
		mapView = (MapView)findViewById(R.id.mapView);
		mapView.setBuiltInZoomControls(true);
		
		loadGps();
		
		List<Overlay> mapOverlays = mapView.getOverlays();
		Drawable drawable = this.getResources().getDrawable(R.drawable.ic_launcher);
		MyItemizedOverlay itemizedoverlay = new MyItemizedOverlay(drawable,this);
		
		GeoPoint point = new GeoPoint((int)(location.getLatitude()*1E6), (int)(location.getLongitude()*1E6));
		OverlayItem overlayitem = new OverlayItem(point, "Hi Pndol!", "i'm in Gangju");
		itemizedoverlay.addOverlay(overlayitem);
		
		mapController = mapView.getController();
		mapController.setCenter(point);
		mapController.setZoom(15);
		mapOverlays.add(itemizedoverlay);
	}

	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}
	
	public void loadGps(){
		Log.w(TAG,"LoadGps Start");
		String context = Context.LOCATION_SERVICE;
		locationManager = (LocationManager)getSystemService(context);
		Criteria criteria = new Criteria();
		criteria.setAccuracy(Criteria.ACCURACY_FINE);
		criteria.setPowerRequirement(Criteria.POWER_LOW);
		criteria.setAltitudeRequired(false);
		criteria.setBearingRequired(false);
		criteria.setSpeedRequired(false);
		criteria.setCostAllowed(true);
		
		provider = locationManager.getBestProvider(criteria, true);
		Log.i(TAG,"Provider:"+provider);
		if(provider == null){
			result = chkGpsService();
			if(result){
				loadGps();
			}
		}
		else{
			location = locationManager.getLastKnownLocation(provider);
			locationManager.requestLocationUpdates(provider, 1000, 5, loclistener);
			if(location == null){
				location = locationManager.getLastKnownLocation(provider);
				if(location == null){
					Log.w(TAG,"get Location From GPS Fail!!");
					AlertDialog.Builder adb = new AlertDialog.Builder(ActivityGoogleMap02.this);
					adb.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
						
						@Override
						public void onClick(DialogInterface dialog, int which) {
							// TODO Auto-generated method stub						
						}
					});
					adb.setTitle(R.string.Location_fail_title);
					adb.setMessage(R.string.Location_fail_message);
					adb.show();
				}
			}
		}
	}
	
	private final LocationListener loclistener = new LocationListener() {
		
		@Override
		public void onStatusChanged(String provider, int status, Bundle extras) {
			// TODO Auto-generated method stub
			Log.w(TAG,"onStatusChanged");
		}
		
		@Override
		public void onProviderEnabled(String provider) {
			// TODO Auto-generated method stub
			Log.w(TAG,"onProviderEnabled");
		}
		
		@Override
		public void onProviderDisabled(String provider) {
			// TODO Auto-generated method stub
			Log.w(TAG,"onProviderDisabled");
		}
		
		@Override
		public void onLocationChanged(Location location) {
			// TODO Auto-generated method stub
			Log.w(TAG,"onLocationChanged");
		}
	};
	
	private boolean chkGpsService(){
		String gs = android.provider.Settings.Secure.getString(getContentResolver(), 
				android.provider.Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
		Log.w("chkGpsService","get GPs Service");
		
		if(gs.indexOf("gps",0)<0){
			Log.w("chkGpsService","status: off");
			AlertDialog.Builder gsDialog = new AlertDialog.Builder(this);
			gsDialog.setTitle("GPS Stauts OFF!!");
			gsDialog.setMessage("Change Setting!!");
			gsDialog.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
					Intent intent = new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
					intent.addCategory(Intent.CATEGORY_DEFAULT);
					startActivity(intent);
				}
			}).create().show();
			return false;
		}
		else{
			Log.w("chkGpsService","status:on");
			return true;
		}
	}
}